﻿using System;
using System.Collections;
using System.Globalization;
using mojoPortal.Web;
using mojoPortal.Web.Framework;
using mojoPortal.Business;
using eStore.Business;
using mojoPortal.Business.WebHelpers;
using Resources;

namespace eStore.UI
{

    public partial class ProductListPage : mojoBasePage
    {
        protected Store store;
        protected CultureInfo currencyCulture = CultureInfo.CurrentCulture;
        private bool enableRatingsInProductList;
        private bool enableRatingComments;
        private Hashtable Settings;
        private Module module;
        private int pageId = -1;
        private int moduleId = -1;
        private int categoryID = -1;
        eStoreConfiguration config = new eStoreConfiguration();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!UserCanViewPage(moduleId))
            {
                SiteUtils.RedirectToAccessDeniedPage();
                return;
            }

            LoadSettings();
            if ((store == null) || (store.IsClosed))
            {
                WebUtils.SetupRedirect(this, SiteUtils.GetCurrentPageUrl());
                return;
            }

            PopulateLabels();
            PopulateControls();

        }

        private void PopulateControls()
        {


        }


        private void PopulateLabels()
        {
            Title = SiteUtils.FormatPageTitle(siteSettings, eStoreResources.ProductListHeading);

            TitleControl.EditUrl = SiteRoot + "/eStore/AdminDashboard.aspx";
            TitleControl.EditText = eStoreResources.StoreManagerLink;
            if (module != null) { TitleControl.ModuleInstance = module; }
            TitleControl.CanEdit = UserCanEditModule(moduleId);
            pnlCartLink.Visible = config.ShowCartLinkSetting;
        }

        private void LoadSettings()
        {
            siteSettings = CacheHelper.GetCurrentSiteSettings();
            store = new Store(siteSettings.SiteGuid, moduleId);
            module = GetModule(moduleId);
            lnkCart.PageID = pageId;
            lnkCart.ModuleID = moduleId;

            enableRatingsInProductList = WebUtils.ParseBoolFromHashtable(
                Settings, "EnableContentRatingInProductListSetting", enableRatingsInProductList);

            enableRatingComments = WebUtils.ParseBoolFromHashtable(
                Settings, "EnableRatingCommentsSetting", enableRatingComments);

            pnl1.CssClass += " " + config.CustomCssClassSetting;

            productList1.Store = store;
            productList1.PageId = pageId;
            productList1.ModuleId = moduleId;
            productList1.SiteRoot = SiteRoot;
            productList1.CurrencyCulture = currencyCulture;
            productList1.EnableRatings = enableRatingsInProductList;
            productList1.EnableRatingComments = enableRatingComments;
            productList1.Settings = Settings;
            productList1.Config = config;
            productList1.PageSize = config.PageSizeSetting;
            productList1.CategoryID = categoryID;
        }

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", moduleId);
            categoryID = WebUtils.ParseInt32FromQueryString("catid", categoryID);
        }

        private void LoadPanels()
        {
            Settings = ModuleSettings.GetModuleSettings(moduleId);
            config = new eStoreConfiguration(Settings);
            LoadSideContent(config.ShowLeftPanelSetting, config.ShowRightPanelSetting, config.ShowTopPanelSetting, config.ShowBottomPanelSetting, config.ShowCenterPanelSetting);
        }

        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
        }

        override protected void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            base.OnInit(e);
            Load += Page_Load;
        }

        #endregion
    }
}
